package com.weixin.servlet.information;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.weixin.service.IPayService;
import com.weixin.service.impl.PayServiceImpl;
import com.weixin.util.PageUtil;
import com.weixin.vo.Pay;
import com.weixin.vo.User;

/**
 * Servlet implementation class PayServlet
 */
@WebServlet("/PayServlet/*")
public class PayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	IPayService payService = new PayServiceImpl();
       
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pathInfo = request.getPathInfo().substring(1);
		if("listAll".equals(pathInfo)) {
			listAll(request,response);
		}else if("findAllBySplit".equals(pathInfo)) {
			findAllBySplit(request,response);
		}else if("findRecom".equals(pathInfo)) {
			findRecom(request,response);
		}else if("findXiaofei".equals(pathInfo)) {
			findXiaofei(request,response);
		}else if("add".equals(pathInfo)) {
			add(request,response);
		}else if("edit".equals(pathInfo)) {
			edit(request,response);
		}else if("preEdit".equals(pathInfo)) {
			preEdit(request,response);
		}else if("removeById".equals(pathInfo)) {
			removeById(request,response);
		}
	}
	
	/**
	 * ���Ѽ�¼��ѯɾ��
	 * @param request
	 * @param response
	 */
	private void removeById(HttpServletRequest request, HttpServletResponse response) {
		String[] pay = request.getParameterValues("pay_id");
				
				//ɾ������
				try {
					if(payService.removeById(pay)) {
						response.getWriter().println(1);
					}else {
						response.getWriter().println(0);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
	}

	/**
	 * ���Ѽ�¼��ѯԤ�޸�
	 * @param request
	 * @param response
	 */
	private void preEdit(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int pay_id = 0;
		if(request.getParameter("pay_id")!=null&&!(request.getParameter("pay_id").equals(""))) {
			pay_id = Integer.valueOf(request.getParameter("pay_id"));
		}
		try {
			//���ݹ���id�ҵ�������Ϣ
			Pay pay = payService.findById(pay_id);
			
			if(pay!=null) {
				response.getWriter().println("["+new Gson().toJson(pay)+"]");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * ���Ѽ�¼��ѯ�޸ĵ���
	 * @param request
	 * @param response
	 */
	private void edit(HttpServletRequest request, HttpServletResponse response) {
		//���ñ����ʽ
				try {
					request.setCharacterEncoding("utf-8");
					//��jsp�õ�ֵ
					String user_name = request.getParameter("user_name");
					String pay_id = request.getParameter("pay_id");
					int pay_count = Integer.valueOf(request.getParameter("pay_count"));
					int integral = pay_count/10;
					//����ֵ
					Pay vo = new Pay();
					vo.setUser_name(user_name);
					vo.setPay_count(pay_count);
					vo.setPay_time(new Date());
					vo.setPay_id(pay_id);
					vo.setIntegral(integral);
					//�ض�������ҳ����
					if(payService.edit(vo)) {
						response.sendRedirect(request.getContextPath()+"/PayServlet/findAllBySplit?pageNum=1");
					}else {
						
					}
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	}

	/**
	 * ���Ѽ�¼��ѯ��ӵ���
	 * @param request
	 * @param response
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) {
		try {
			//���ñ����ʽ
			request.setCharacterEncoding("utf-8");
			//��jsp�õ�������ֵ
			String user_name = request.getParameter("user_name");
			int pay_count = Integer.valueOf(request.getParameter("pay_count"));
			int integral = pay_count/10;
			Pay vo = new Pay();
			//����
			vo.setUser_name(user_name);
			vo.setPay_count(pay_count);
			vo.setPay_time(new Date());
			vo.setIntegral(integral);
			//�����Ƿ����гɹ��������������Ϣ
			boolean b = payService.add(vo);
			if(b) {
				//�ض�����findAllBySplit����
				response.sendRedirect(request.getContextPath()+"/PayServlet/findAllBySplit?pageNum=1");
			}else{
				
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * ���Ѽ�¼��ѯ֮����ҳ�Ĳ�ѯ
	 * @param request
	 * @param response
	 */
	private void findXiaofei(HttpServletRequest request, HttpServletResponse response) {
		Map<String, String[]> parameterMap = request.getParameterMap();
		int pageNum = 1;
        if(request.getParameter("pageNum")!=null){
           pageNum = Integer.valueOf(request.getParameter("pageNum"));
        }
	try {
		List<Pay> payList = payService.findXiaofei(pageNum, PageUtil.PAGESIZE, parameterMap);
        request.setAttribute("payList",payList);
        //�����
        request.setAttribute("parameterMap",parameterMap);
        //ת����jsp����ҳ��
		request.getRequestDispatcher("/pages/information/consumeQuery_list.jsp").forward(request, response);
		
	} catch (Exception e) {
		e.printStackTrace();
	}
}
	

	/**
	 * ��ѯ�Ƽ��˷���
	 * @param request
	 * @param response
	 */
	private void findRecom(HttpServletRequest request, HttpServletResponse response) {
				try {
					//���ñ����ʽ
					request.setCharacterEncoding("utf-8");
					//��jsp��õ�user_tuijianֵ
					String user_tuijian = request.getParameter("user_tuijian");
					Pay vo = new Pay();
					//����user_tuijianֵ
					vo.setUser_tuijian(user_tuijian);
					//�ж�ȡ����ֵ�Ƿ�Ϊ��
					List<Pay> findRecom = payService.findRecom(vo);
					if(findRecom!=null) {
						request.setAttribute("findRecom", findRecom);
						request.getRequestDispatcher("/pages/information/recommendQuery2_list.jsp").forward(request, response);
						
					}else {
						request.getRequestDispatcher("/pages/information/recommendQuery_list.jsp").forward(request, response);
					}
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
	}
	/**
	 * ��ҳ����
	 * @param request
	 * @param response
	 */
	private void findAllBySplit(HttpServletRequest request, HttpServletResponse response) {
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		try {
			//��÷�ҳ����
			List<Pay> payList = payService.findAllBySplit(pageNum, PageUtil.PAGESIZE);
			//���������
			int total = payService.findTotal();
			//������ҳ��
			int pageCount = (int) Math.ceil(total*1.0/PageUtil.PAGESIZE);
			//request��������Գ�Աlist
			request.setAttribute("payList", payList);
			request.setAttribute("pageCount", pageCount);
			request.setAttribute("pageNum", pageNum);
			//ת��������ҳ��
			request.getRequestDispatcher("/pages/information/consumeQuery_list.jsp").forward(request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * �������û����
	 * @param request
	 * @param response
	 */
	private void listAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			//����ҵ����õ�����
			List<Pay> pay = payService.selectPay();
			//request��������Գ�Աlist
			request.setAttribute("payList", pay);
			//ת��������ҳ��
			request.getRequestDispatcher("/pages/information/consumeQuery_list.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



}
